﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;
using System.Data.SqlClient;

namespace ESCHOOL.admin
{
    public partial class WebForm9 : System.Web.UI.Page
    {
        gallery gal = new gallery();
        album al = new album();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            if (!IsPostBack)
            {
                DataTable dt1 = al.GetRecords(-99, "", "", false, " and "); 
                ddlalbum.DataSource = dt1;
                ddlalbum.DataTextField = "album_name";
                ddlalbum.DataValueField = "album_id";
                ddlalbum.DataBind();
                
                DataTable dt = gal.GetRecords(-99, -99, "", "", false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            DataTable dt=gal.GetRecords(Convert.ToInt64(e.CommandArgument),-99,"","",false," and ");
            tbgalleryid.Text = dt.Rows[0]["gallery_id"].ToString();
            ddlalbum.SelectedValue = dt.Rows[0]["album_id"].ToString();
            TextBox1.Text = dt.Rows[0]["image_name"].ToString();
            tbdescrip.Text = dt.Rows[0]["image_description"].ToString();

        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = gal.GetRecords(Convert.ToInt64(e.CommandArgument), -99, "", "", false, " and ");
                gal.gallery_id = Convert.ToInt64(dt.Rows[0]["gallery_id"].ToString());
                gal.album_id = Convert.ToInt64(dt.Rows[0]["album_id"].ToString());
                gal.image_name = dt.Rows[0]["image_name"].ToString();
                gal.image_description = dt.Rows[0]["image_description"].ToString();
                gal.isdeleted = true;
                gal.SaveRecords();
                Response.Redirect("~/admin/Gallery.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            gal.gallery_id = -99;
            gal.album_id = Convert.ToInt64(ddlalbum.SelectedValue.ToString());
            if (FileUpload1.HasFile)
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/gallery/" + FileUpload1.FileName));
            gal.image_name = FileUpload1.FileName;
            gal.image_description = tbdescrip.Text;
            gal.isdeleted = false;
            gal.SaveRecords();
            Response.Redirect("~/admin/Gallery.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            gal.gallery_id = Convert.ToInt64(tbgalleryid.Text);
            gal.album_id = Convert.ToInt64(ddlalbum.SelectedValue.ToString());
            if(FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/gallery/"+FileUpload1.FileName));
                gal.image_name=FileUpload1.FileName;
            }
            else
                gal.image_name = TextBox1.Text;
            gal.image_description = tbdescrip.Text;
            gal.isdeleted = false;
            gal.SaveRecords();
            Response.Redirect("~/admin/Gallery.aspx");
        }

        protected void ddlalbum_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
    }
}
